﻿using System.IO;
using Gadgeteer.Modules.DotNetWarrior;
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace TestApp
{
    public partial class Program
    {
        private const string TRASHTHIS = "DeleteThis.txt";
        private readonly FlashFileSystem flashFileSystem = new FlashFileSystem(6);
        // This method is run when the mainboard is powered up or reset.   
        private void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/


            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            if (!flashFileSystem.CheckIfFormatted())
            {
                flashFileSystem.Format();
            }

            flashFileSystem.Mount();

            var newfile = "file";
            var count = 0;

            while (flashFileSystem.Exists(newfile + count + ".txt"))
            {
                count++;
            }

            newfile = newfile + count + ".txt";

            var openfile = flashFileSystem.Create(newfile);

            var stream = new StreamWriter(openfile);

            stream.WriteLine("Hello World");

            stream.Close();
            openfile.Close();

            flashFileSystem.Copy(newfile, TRASHTHIS);

            var fileNames = flashFileSystem.GetFiles();

            foreach (var fileName in fileNames)
            {
                var size = flashFileSystem.GetFileSize(fileName);
                var creation = flashFileSystem.GetFileCreationTime(fileName);
                Debug.Print(fileName + " " + size + " " + creation);
            }

            openfile = flashFileSystem.Open(newfile, FileMode.Open);
            var reader = new StreamReader(openfile);

            var message = reader.ReadLine();
            Debug.Print(message);

            reader.Close();
            stream.Close();

            flashFileSystem.Delete(TRASHTHIS);

            if (flashFileSystem.Exists(TRASHTHIS))
                Debug.Print("Delete Failed");
            else
            {
                Debug.Print("Delete success");
            }

            flashFileSystem.Compact();
        }
    }
}